package org.jfonia.connect5.collections;

import org.jfonia.connect5.basics.Node;
import org.jfonia.connect5.basics.Observer;


/**
 * @author wijnand.schepens@hogent.be
 */
public class SetSynchronizer<S, T> implements Observer
{
	protected SetNode<S> set1;
	protected SetNode<T> set2;

	public SetSynchronizer(SetNode<S> set1, SetNode<T> set2)
	{
		this.set1 = set1;
		this.set2 = set2;

		set1.getAdditionNode().addObserver(this);
		set1.getRemovalNode().addObserver(this);
		set2.getAdditionNode().addObserver(this);
		set2.getRemovalNode().addObserver(this);
	}

	public void onNotify(Object source)
	{
		if (source == set1.getAdditionNode())
			onAddToSet1(set1.getAdditionNode().getValue());
		else if (source == set1.getRemovalNode())
			onRemoveFromSet1(set1.getRemovalNode().getValue());
		else if (source == set2.getAdditionNode())
			onAddToSet2(set2.getAdditionNode().getValue());
		else if (source == set2.getRemovalNode())
			onRemoveFromSet2(set2.getRemovalNode().getValue());
	}

	protected void onAddToSet1(S x)
	{
		set2.add(transform1to2(x), this);
	}

	protected void onAddToSet2(T y)
	{
		set1.add(transform2to1(y), this);
	}

	protected void onRemoveFromSet1(S x)
	{
		set2.remove(transform1to2(x), this);
	}

	protected void onRemoveFromSet2(T y)
	{
		set1.remove(transform2to1(y), this);
	}


	protected T transform1to2(S x)
	{
		return null;
	}

	protected S transform2to1(T y)
	{
		return null;
	}

}
